package com.aizhixin.lab.resource.service;

import com.aizhixin.lab.common.dto.SortDTO;
import com.aizhixin.lab.common.utils.PageJdbcUtil;
import com.aizhixin.lab.course.course.entity.CourseAssist;
import com.aizhixin.lab.course.course.repository.CourseAssistRepository;
import com.aizhixin.lab.course.template.entity.TemplateAssist;
import com.aizhixin.lab.course.template.repository.TemplateAssistRepository;
import com.aizhixin.lab.resource.domain.FileInfoDomain;
import com.aizhixin.lab.resource.domain.FileTotalNumDomain;
import com.aizhixin.lab.resource.entity.DirFile;
import com.aizhixin.lab.resource.repository.DirFileRepository;
import com.aizhixin.lab.resource.repository.DirRepository;
import com.aizhixin.lab.task.domain.QueryDomain;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
@Transactional
public class AssistService {
    @Autowired
    private TemplateAssistRepository templateAssistRepository;
    @Autowired
    private CourseAssistRepository courseAssistRepository;
    @Autowired
    private DirRepository dirRepository;
    @Autowired
    private DirFileRepository dirFileRepository;
    @Autowired
    private PageJdbcUtil pageJdbcUtil;
    RowMapper<FileInfoDomain> assistCouFileRm = new RowMapper<FileInfoDomain>() {

        @Override
        public FileInfoDomain mapRow(ResultSet rs, int rowNum) throws SQLException {
            // TODO Auto-generated method stub
            FileInfoDomain domain = new FileInfoDomain();
            domain.setId("C_" + rs.getString("ID"));
            domain.setFileName(rs.getString("file_name"));

            domain.setCourseName(rs.getString("course_name"));
            domain.setCreateDate(rs.getDate("created_date"));

            return domain;
        }
    };

    /**
     * 获取课程下的辅助文件
     *
     * @param domain
     * @param <T>
     * @return
     */
    public <T> Map<String, Object> getCourseAssist(QueryDomain domain) {
        String sql = "SELECT cca.id,cca.`name` file_name,cc.`name` course_name, cca.created_date FROM c_course_assist cca LEFT JOIN c_course cc ON cca.course_id=cc.id WHERE cca.delete_flag=0  ";
        String countSql = "SELECT COUNT(*) FROM c_course_assist cca LEFT JOIN c_course cc ON cca.course_id=cc.id WHERE cca.delete_flag=0 ";
        if (StringUtils.isEmpty(domain.getId())) {
            String[] s = domain.getId().split("_");
            sql += " and cc.id= " + s[1];
            countSql += " and cc.id= " + s[1];
        }
        List<SortDTO> sort = new ArrayList<SortDTO>();
        SortDTO dto = new SortDTO();
        dto.setAsc(false);
        dto.setKey("cca.created_date");
        sort.add(dto);

        return pageJdbcUtil.getPageInfor(domain.getPageSize(), domain.getPageNumber(), assistCouFileRm, sort, sql, countSql);
    }

    RowMapper<FileInfoDomain> assistTemFileRm = new RowMapper<FileInfoDomain>() {

        @Override
        public FileInfoDomain mapRow(ResultSet rs, int rowNum) throws SQLException {
            // TODO Auto-generated method stub
            FileInfoDomain domain = new FileInfoDomain();
            domain.setId("T_" + rs.getLong("ID"));
            domain.setFileName(rs.getString("file_name"));

            domain.setCourseName(rs.getString("template_name"));
            domain.setCreateDate(rs.getDate("created_date"));

            return domain;
        }
    };

    /**
     * 获取模板下的辅助文件
     *
     * @param domain
     * @param <T>
     * @return
     */
    public <T> Map<String, Object> getTemplateAssist(QueryDomain domain) {
        String sql = "SELECT cca.id,cca.`name` file_name,cc.`name` template_name, cca.created_date FROM c_template_assist cca LEFT JOIN c_template cc ON cca.template_id =cc.id WHERE cca.delete_flag=0  ";
        String countSql = "SELECT COUNT(*) FROM c_template_assist cca LEFT JOIN c_template cc ON cca.template_id =cc.id WHERE cca.delete_flag=0 ";
        if (StringUtils.isEmpty(domain.getId())) {
            String[] s = domain.getId().split("_");
            sql += " and cc.id= " + Long.parseLong(s[1]);
            countSql += " and cc.id= " + Long.parseLong(s[1]);
        }
        List<SortDTO> sort = new ArrayList<SortDTO>();
        SortDTO dto = new SortDTO();
        dto.setAsc(false);
        dto.setKey("cca.created_date");
        sort.add(dto);

        return pageJdbcUtil.getPageInfor(domain.getPageSize(), domain.getPageNumber(), assistTemFileRm, sort, sql, countSql);
    }

    /**
     * 搜索课程辅助资料
     * @param domain
     * @param <T>
     * @return
     */
    public <T> Map<String, Object> searchAssist(QueryDomain domain) {
    String sql="SELECT cca.id,cca.`name` file_name,cc.`name` template_name, cca.created_date FROM c_template_assist cca LEFT JOIN c_template cc ON cca.template_id =cc.id WHERE cca.delete_flag=0 ";
    String countSql="SELECT count(*) FROM c_template_assist cca LEFT JOIN c_template cc ON cca.template_id =cc.id WHERE cca.delete_flag=0";
        if (StringUtils.isEmpty(domain.getName())) {
            sql+=" AND cca.`name` LIKE '%"+domain.getName()+"%'";
            countSql+=" AND cca.`name` LIKE '%"+domain.getName()+"%'";
        }
        List<SortDTO> sort = new ArrayList<SortDTO>();
        SortDTO dto = new SortDTO();
        dto.setAsc(false);
        dto.setKey("cca.created_date");
        sort.add(dto);
        return pageJdbcUtil.getPageInfor(domain.getPageSize(), domain.getPageNumber(), assistTemFileRm, sort, sql, countSql);
    }
    /**
     * 删除辅助资料
     *
     * @param fileNoteId
     */
    public Integer deleteAssistFile(String fileNoteId) {
        String[] s = fileNoteId.split("_");
        Integer row = 0;
        if ("D".equals(s[0])) {

            DirFile dirFile = dirFileRepository.findById(Long.parseLong(s[1]));
            if (dirFile != null) {
                dirFile.setDeleteFlag(1);
                DirFile save = dirFileRepository.save(dirFile);

                return 1;
            }
        }
        if ("T".equals(s[0])) {
            TemplateAssist byId = templateAssistRepository.findById(Long.parseLong(s[1]));
            if (byId != null) {
                byId.setDeleteFlag(1);
                templateAssistRepository.save(byId);
                return 1;
            }
        }
        if ("C".equals(s[0])) {
            CourseAssist byId = courseAssistRepository.findById(Long.parseLong(s[1]));
            if (byId != null) {
                byId.setDeleteFlag(1);
                courseAssistRepository.save(byId);
                return 1;
            }
        }
        return 0;
    }

    public Integer getFileTotalNumDomain() {
        Integer integer = templateAssistRepository.countByDeleteFlag(0);
//        Integer integer1 = courseAssistRepository.countByDeleteFlag(0);
        Integer integer2 = dirFileRepository.countByTypeAndDeleteFlag(60, 0);
        return integer + integer2;
    }
    public TemplateAssist getTemplateAssist(Long id){
        return templateAssistRepository.findById(id);
    }
    public CourseAssist getCourseAssist(Long id){
        return courseAssistRepository.findById(id);
    }
    public TemplateAssist updateTemplateAssist(Long id,String fileName){
        TemplateAssist byId = templateAssistRepository.findById(id);
        if (byId!=null) {
            byId.setName(fileName);
            templateAssistRepository.save(byId);
        }
        return byId;
    }

    public CourseAssist updateCourseAssist(Long id,String fileName){
        CourseAssist byId = courseAssistRepository.findById(id);
        if (byId!=null) {
            byId.setName(fileName);
            courseAssistRepository.save(byId);
        }
        return byId;
    }

}
